package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//Servlet 是一个特殊的Java 类
//  可以借助java代码实现在网页页面输出结果,也可以用java逻辑(if for)
// 注解 简化配置 原本要写在xml的配置文件的代码,可以直接写在类或方法的声明之前
@WebServlet(urlPatterns ={"/hello","/t321","/xxw","/doLogin"} )
public class HelloServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>初始化");
     //   super.init();// 初始化
    }

  /*  @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-------------service---------------------");
        super.service(req, resp);
    }*/

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------------get -------------------------");
       // super.doGet(req, resp);
        String uname = request.getParameter("username");
        String password = request.getParameter("password");
        if ("admin".equals(uname) && "123456".equals(password)){
            request.getSession().setAttribute("currUser",uname);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }else{
            response.sendRedirect("login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------------post  -------------------------");
        doGet(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("-------------------销毁----------------------------");
        super.destroy();
    }
}
